package com.bilgeadam.hibernate.program;

import java.util.List;
import java.util.Random;

import javax.persistence.NamedQuery;

import javafx.scene.control.Pagination;
import oracle.net.aso.e;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.hql.internal.ast.tree.FromClause;
import org.hibernate.hql.internal.ast.tree.OrderByClause;

import com.sun.xml.internal.ws.api.model.ParameterBinding;

/**
 * 
 * @author Bora SAYINER
 * @since Nov 15, 2014 3:57:55 PM
 * @version 1.0.0
 */
@SuppressWarnings("unchecked")
public class Driver {

	public static void main(String[] args) {
		SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
		Session session = sessionFactory.openSession();
		Transaction transaction = session.beginTransaction();
		{
			// fillDatabase(session);
			// fromClause(session);
			// asClause(session);
			// selectClause(session);
			// whereClause(session);
			// orderByClause(session);
			// groupByClause(session);
			// parameterBinding(session);
			// pagination(session);
			namedQuery(session);
		}

		transaction.commit();
		session.close();
		sessionFactory.close();
	}

	private static void namedQuery(Session session) {
		Query query = session.getNamedQuery("Employees.ById");
		query.setParameter(0, 5);
		Employee employee = (Employee) query.uniqueResult();
		System.out.println(employee);
	}

	private static void pagination(Session session) {
		Query query = session.createQuery("FROM Employee");
		query.setFirstResult(4 * 5);
		query.setMaxResults(5);
		List<Employee> employees = query.list();
		for (Employee employee : employees) {
			System.out.println(employee);
		}
	}

	private static void parameterBinding(Session session) {
		Query query = session.createQuery("FROM Employee WHERE salary > :salary");
		query.setParameter("salary", 700D);
		List<Employee> employees = query.list();
		for (Employee employee : employees) {
			System.out.println(employee);
		}
	}

	private static void groupByClause(Session session) {
		Query query = session.createQuery("SELECT SUM(e.salary), e.firstName FROM Employee AS e GROUP BY e.firstName");
		List<Object[]> values = query.list();
		for (Object[] value : values) {
			System.out.println(value[0] + "," + value[1]);
		}

	}

	private static void orderByClause(Session session) {
		Query query = session.createQuery("FROM Employee WHERE salary > 700 ORDER BY salary DESC");
		List<Employee> employees = query.list();
		for (Employee employee : employees) {
			System.out.println(employee);
		}
	}

	private static void whereClause(Session session) {
		Query query = session.createQuery("FROM Employee WHERE salary > 700");
		List<Employee> employees = query.list();
		for (Employee employee : employees) {
			System.out.println(employee);
		}
		System.out.println("Total Count: " + employees.size());
	}

	private static void selectClause(Session session) {
		Query query = session.createQuery("SELECT e.firstName, e.lastName FROM Employee AS e");
		List<Object[]> values = query.list();
		for (Object[] value : values) {
			System.out.println(value[0] + ", " + value[1]);
		}
	}

	private static void asClause(Session session) {
		Query query = session.createQuery("FROM Employee AS e");
		List<Employee> employees = query.list();
		for (Employee employee : employees) {
			System.out.println(employee);
		}
	}

	private static void fromClause(Session session) {
		Query query = session.createQuery("FROM Employee");
		List<Employee> employees = query.list();
		for (Employee employee : employees) {
			System.out.println(employee);
		}
	}

	private static void fillDatabase(Session session) {
		Random random = new Random();
		for (int i = 0; i < 1000; i++) {
			Employee employee = new Employee();
			employee.setFirstName(String.format("First Name %d", i));
			employee.setLastName(String.format("Last Name %d", i));
			employee.setSalary(random.nextDouble() * 1000);
			session.save(employee);
		}
	}
}
